package cn.gcyl.module.biz.service.informationtype;

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.gcyl.module.biz.controller.admin.informationtype.vo.*;
import cn.gcyl.module.biz.dal.dataobject.informationtype.InformationTypeDO;
import cn.gcyl.framework.common.pojo.PageResult;
import cn.gcyl.framework.common.pojo.PageParam;
import cn.gcyl.framework.common.util.object.BeanUtils;

import cn.gcyl.module.biz.dal.mysql.informationtype.InformationTypeMapper;

import static cn.gcyl.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.gcyl.module.system.enums.ErrorCodeConstants.INFORMATION_TYPE_NOT_EXISTS;

/**
 * 信息类型 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class InformationTypeServiceImpl implements InformationTypeService {

    @Resource
    private InformationTypeMapper informationTypeMapper;

    @Override
    public Integer createInformationType(InformationTypeSaveReqVO createReqVO) {
        // 插入
        InformationTypeDO informationType = BeanUtils.toBean(createReqVO, InformationTypeDO.class);
        informationTypeMapper.insert(informationType);

        // 返回
        return informationType.getId();
    }

    @Override
    public void updateInformationType(InformationTypeSaveReqVO updateReqVO) {
        // 校验存在
        validateInformationTypeExists(updateReqVO.getId());
        // 更新
        InformationTypeDO updateObj = BeanUtils.toBean(updateReqVO, InformationTypeDO.class);
        informationTypeMapper.updateById(updateObj);
    }

    @Override
    public void deleteInformationType(Integer id) {
        // 校验存在
        validateInformationTypeExists(id);
        // 删除
        informationTypeMapper.deleteById(id);
    }

    @Override
        public void deleteInformationTypeListByIds(List<Integer> ids) {
        // 删除
        informationTypeMapper.deleteByIds(ids);
        }


    private void validateInformationTypeExists(Integer id) {
        if (informationTypeMapper.selectById(id) == null) {
            throw exception(INFORMATION_TYPE_NOT_EXISTS);
        }
    }

    @Override
    public InformationTypeDO getInformationType(Integer id) {
        return informationTypeMapper.selectById(id);
    }

    @Override
    public PageResult<InformationTypeDO> getInformationTypePage(InformationTypePageReqVO pageReqVO) {
        return informationTypeMapper.selectPage(pageReqVO);
    }

}